﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PRODUCT.Product.database;
using System.Text.RegularExpressions;

namespace ERP_RORZE_PRODUCT.Product.BasicList
{
    public partial class frm_OvertimeEdit : DevExpress.XtraEditors.XtraForm
    {

        public event EventHandler Reload;
        ClsBasicListDAO clsBasiclistDAO = new ClsBasicListDAO();
        ClsOvertime b = new ClsOvertime();

        public string ID;
        public string Date;
        public string Staff;
        public string Time;
        private LogUtils log;

        public frm_OvertimeEdit()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtTime.Text == "")
            {
                MessageBox.Show("Chưa nhập Time");
            }
            else if (IsNumber(txtTime.Text) == false)
            {
                MessageBox.Show("Time phải là số");
            }
            else
            {
                b.Date = dateDate.Value;
                b.StaffID = cbStaffID.SelectedValue.ToString();
                b.Time = int.Parse(txtTime.Text);
                clsBasiclistDAO.UpdateOvertime(b);

                //write Log
                log.Table = LogConstant.Table.Product.OverTime;
                log.Task = LogConstant.Task.Update;
                log.Description = "Date: " + b.Date + ", StaffID: " + b.StaffID + ", Time: " + b.Time;
                log.WriteLog();

                MessageBox.Show("Đã nhập xong");
                Reload(sender, e);
            }
        }
        public bool IsNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }

        private void frm_OvertimeEdit_Load(object sender, EventArgs e)
        {
            DataTable dt;
            ClsWTSDAO clsWTSDAO = new ClsWTSDAO();
            dt = clsWTSDAO.GetAllStaff();
            DataRow r = dt.NewRow();
            r["StaffID"] = "";
            r["FullName"] = "";
            dt.Rows.InsertAt(r, 0);
            cbStaffID.DataSource = dt;
            cbStaffID.DisplayMember = "FullName";
            cbStaffID.ValueMember = "StaffID";

            cbStaffID.SelectedValue = Staff;
            dateDate.Value = DateTime.Parse(Date);
            txtTime.Text = Time;
        }
    }
}